// pages/scandetail/scandetail.js
const app = getApp()
var that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [
      { sku: '1001', name: '商品1', stock: 50 },
      { sku: '1002', name: '商品2', stock: 30 },
      { sku: '1003', name: '商品3', stock: 40 },
      { sku: '1004', name: '商品4', stock: 60 },
      { sku: '1005', name: '商品5', stock: 80 },
      { sku: '1006', name: '商品6', stock: 20 },
      { sku: '1007', name: '商品7', stock: 100 },
      { sku: '1008', name: '商品8', stock: 150 },
      { sku: '1009', name: '商品9', stock: 90 },
      { sku: '1010', name: '商品10', stock: 120 }
    ],
    q: '',
    cart: [],
    money: 0,
    popTop: 0,
    num: '',
    sn: '',
    isButtonDisabled: false, // 按钮的禁用状态
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(query) {
    var scanCode = query.scanCode;
    console.log(app.globalData.baseSn)
    console.log("query-==",scanCode)
    var baseSn;
    if(scanCode != null && scanCode !=''){
       baseSn = scanCode;
    }else{
      const q = decodeURIComponent(query.q) // 获取到二维码原始链接内容
      console.log("q==",q)
      let _i1 = q.split('?')[1]
      let _i2 = _i1.split('=')[1]
      baseSn = _i2;
    }
    app.globalData.baseSn = baseSn;
    this.setData({
      sn:baseSn
    })
    // app.globalData.baseSn = '865074057782194'
    that = this
  },

  // 处理库存值的输入变化
  onStockInputChange(e) {
    const { index } = e.currentTarget.dataset; // 获取当前编辑项的索引
    const value = e.detail.value;             // 获取输入的值

    // 更新对应商品的库存值
    const key = `list[${index}].goodsStock`;
    this.setData({
      [key]: value,
    });
  },

  //获取商品和库存列表
  getList(){

    that.requestHttp('/app/client/qrcode/replenishment', {deviceSn:app.globalData.baseSn}, 'get').then((data) => {
      console.log(data.rows)
      this.setData({
        list: data.rows.map(item => ({
          ...item,
          num: 0 // 每项数据初始化时可以加一个 num 字段
        }))
      });
      console.log(that.list)
    })
  },

  oneClick:function(){
    console.log(app.globalData.baseSn)
    if (this.data.isButtonDisabled) return; // 避免重复点击
    // 禁用按钮
    this.setData({
      isButtonDisabled: true,
    });
    this.requestHttp('/app/client/qrcode/replenishAll', {deviceSn:app.globalData.baseSn}, 'get').then((data) => {
      console.log(data)
      if (data.code === 0){
        wx.showToast({
          title: '补满成功',
        })
        this.setData({
          isButtonDisabled: false,
        });
        this.getList()
        //回到上一页
        // wx.navigateBack({
        //   delta: 1, // 返回上一级页面
        //   success: () => {
        //     console.log('返回上一页成功');
        //   },
        //   fail: (err) => {
        //     console.error('返回失败', err);
        //   },
        // });
      }
    })
  },

    // 按钮点击事件
    onButtonClick: function (e) {
      console.log(e.currentTarget.dataset.index)
      const index = e.currentTarget.dataset.index;
      const item = that.data.list[index];
      console.log(item.id)
      console.log(item.goodsStock)
      console.log(item.capacity)
      that.requestHttp('/app/client/qrcode/replenish', {id:item.id,goodsStock:item.goodsStock,capacity:item.capacity}, 'post').then((data) => {
        if (data.code === 0){
          wx.showToast({
            title: '补货成功',
          })
        }
      })
    },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  requestHttp(url,data,method){
    return new Promise(function (reslove, reject) {
      let headers = {
        'Authorization': app.globalData.token || '',
        'Content-Type': 'application/x-www-form-urlencoded'
      };
      wx.request({
        url: app.globalData.host + url,
        method: method || 'get',
        data: data,
        header: headers,
        success: function success(res) {
          const data = res.data;
          if (data.code * 1 === 0) {
            reslove(data)
          } else {
            wx.showToast({
              title: data.msg || '网络异常',
              icon: 'error',
            })
            setTimeout(() => {
              reject(data);
            }, 10)
          }
          // reslove(res)
        },
        fail: function fail(res) {
          wx.showToast({
            title: res,
            icon: 'error',
          })
          reject(res);
        }
      })
    })
  }
})